package cevent.source.cloudcenter.template.generator;/**
 * Created by Cevent on 2021/3/20.
 */

import cevent.source.cloudcenter.template.util.DataBaseUtil;
import cevent.source.cloudcenter.template.util.FieldUtil;
import cevent.source.cloudcenter.template.util.FreemarkerUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

/**
 * @author cevent
 * @description Vue模板应用
 * @date 2021/3/20 23:52
 */
public class VueApplication {
    static String PARENTDIR="cevent-source-cloudcenter";
    static String MODULE="business-core";
    static String MODULE_NAME="business";

    /*static String MODULE="source-center";
    static String MODULE_NAME="source";*/
    /*static String MODULE="system-manager";
    static String MODULE_NAME="system";*/
    static String COMMON="server-common";
    //static String VUE_NAME="vue.ftl";
    static String VUE_NAME="editVue.ftl";
    static String ADD_NAME="Add";
    static String vueTargetPath=PARENTDIR+"\\"+"cevent-ymcms-admin\\src\\views\\main\\module\\";
    static String generatorConfig=PARENTDIR+"\\"+COMMON+"\\src\\main\\resources\\generator\\generatorConfig.xml";

    public static void main(String[] args) throws Exception{
        String module=MODULE_NAME;
        //1.读取xml配置文件
        File file=new File(generatorConfig);
        SAXReader saxReader=new SAXReader();
        //引入dom4j进行读取
        Document document=saxReader.read(file);
        //2.读取xml文件的根节点
        Element rootElement=document.getRootElement();
        //读取context节点
        Element contextElement=rootElement.element("context");
        //3.定义tableElement遍历获取到的表
        Element tableElement;
        tableElement=contextElement.elementIterator("table").next();
        //domainObjectName="Section"
        String Domain=tableElement.attributeValue("domainObjectName");
        String domain=Domain.substring(0,1).toLowerCase()+Domain.substring(1);
        String tableName=tableElement.attributeValue("tableName");
        String tableNameCN= DataBaseUtil.getTableComment(tableName);
        System.out.println("表名："+tableElement.attributeValue("tableName"));
        System.out.println("表构建的Domain："+tableElement.attributeValue("domainObjectName"));

        //4.获取所有列信息
        List<FieldUtil> fieldUtilList=DataBaseUtil.getColumnByTableName(tableName);
        //5.根据类信息，获取所有的set类型集合
        Set<String> typeSet=getJavaTypes(fieldUtilList);
        Map<String,Object> map=new HashMap<>();
        map.put("Domain",Domain);
        map.put("domain",domain);
        map.put("tableNameCN",tableNameCN);
        map.put("module",module);
        map.put("fieldUtilList",fieldUtilList);
        map.put("typeSet",typeSet);

        //6.初始化vue
        FreemarkerUtil.initConfig(VUE_NAME);
        System.out.println("Domain："+Domain);
        //列表模块的vue
        //FreemarkerUtil.generator(vueTargetPath+Domain+".vue",map);
        //编辑模块的vue
        FreemarkerUtil.generator(vueTargetPath+ADD_NAME+Domain+".vue",map);
    }

    /*获取所有的java类型，set去重*/
    private static Set<String> getJavaTypes(List<FieldUtil> fieldUtilList){
        Set<String> set=new HashSet<>();
        for (int i=0;i<fieldUtilList.size();i++){
            FieldUtil fieldUtil=fieldUtilList.get(i);
            set.add(fieldUtil.getJavaType());
        }
        return set;
    }
}
